<?php

class Enquiry_manage  extends AdminManage
{
	private $_pagesize=10;//分页个数
	
	/**
	 * 初始化
	 */
	public function _init()
	{

	}

	/**
	 * 默认动作
	 */
	protected function _default()
	{
		
		#获取配置
		$commonConfig = Config::get('common'); 
		$arrProductType = $commonConfig['common']['product_type'];
		
		$strWhere=' 1 ';
		
		#查询条件处理-start
		 $timeStart = $this->getParam('jts');
         $timeEnd   = $this->getParam('jte');
		 $state   = $this->getParam('state');
		 $uname   = $this->getParam('uname');
		 $keyword   = $this->getParam('kw');
		#查询条件处理-end
		
		
		
			
	    if($timeStart) {
			$datelinesStamp = strtotime($timeStart);
			$strWhere .= ' and sc_enquiry.firstTime >= ' . $datelinesStamp;
			$this->assign('jts',$timeStart);
		}
		if($timeEnd) {
			$datelinedStamp = strtotime($timeEnd." 23:59:59");
			$strWhere .= ' and sc_enquiry.firstTime <= ' . $datelinedStamp;
			$this->assign('jte',$timeEnd);
		}
		
		
		if(is_numeric($state) && in_array($state,array(0,1,2))){
			$strWhere .= ' and sc_enquiry.quoteStatus  = ' . $state;
			$this->assign('state',$state==1?'pass':'nopass');
		}
		
		
 
		if(!empty($keyword)){
			$strWhere .= " and sc_enquiry.projectName  like '%" . $keyword."%'";
			$this->assign('kw',$keyword);
		}
		if(!empty($uname)){
			$sqlUser = "select id from users where username = '".$uname."'";
			$arrUserData = DB::select($sqlUser);
			if($arrUserData){
				$strWhere .= " and sc_enquiry.userId  = " . intval($arrUserData[0]['ID']);
			}else{
				$strWhere .= " and sc_enquiry.userId  = -1";
			}
			$this->assign('username',$uname);
		}



		#获取数据总数
		$total = DB::getRows('sc_enquiry',$strWhere);

		$sql='select sc_enquiry.id ,linkman,projectName,projectType,countryCode,address,sc_enquiry.firstTime
  from sc_enquiry ,sc_enquiry_product  where '.$strWhere.'  and linkman!=\' \'  and sc_enquiry_product.enqId = sc_enquiry.id group by sc_enquiry.id  ';
		$listData=DB::select($sql);
//		print_r($listData);
		$total=	count($listData);




		/**分页操作 start**/
		$p = 1; //当前页码
		if (isset($_GET['p']) && is_numeric($_GET['p'])) {
			$p =intval($_GET['p']);
			if ($p<=1) {
				$p = 1;
			}
		}

		$totalPage = ceil($total/$this->_pagesize);//计算总页数
		$begin = ($p-1)*$this->_pagesize;//计算开始游标
		$limit = "$begin,$this->_pagesize";//计算分页


        if($totalPage > 0){
		  $pageObj = new Dpage($total, $this->_pagesize, $p);
		  $pageCss = $pageObj->pageCss("1");
		  $pageform = $pageObj->outputPages(1,"<li>","</li>");
		}

		/**分页操作 end**/


		#获取数据信息
//		$fields = 'id,userId,projectName,projectType,projectArea,countryCode,address,companyName,linkman,tel,mobile,email,leaveWord,systemId,operateTime,firstTime,quoteStatus';
//		$list = new publicGetDataModel();
//		$listData = $list->getData($where = $strWhere,$limit = $limit,$fields,$order = 'firstTime',$table='sc_enquiry');

		$sql='select sc_enquiry.id ,sc_enquiry.quoteStatus,sc_enquiry.projectArea,linkman,projectName,projectType,countryCode,address,sc_enquiry.firstTime  from sc_enquiry ,sc_enquiry_product
  where '.$strWhere.'  and linkman!=\' \'  and sc_enquiry_product.enqId = sc_enquiry.id group by sc_enquiry.id  ORDER BY sc_enquiry.firstTime desc
limit   '.$limit;
		$listData=DB::select($sql);

		if(empty($listData)) {
			if (isset($key)) {
				$nodatainfo = '没有找到相匹配的新闻信息';
			}
		}


		if($listData){
			include_once(SYSTEM_PATH.'libs/data/province.lib.php');
			foreach($listData  as $k=>$v){
				#工程性质
				if($v['PROJECTTYPE']){
					$listData[$k]['PROJECTTYPENAME'] = $arrProductType[$v['PROJECTTYPE']];
				}
				#状态
				if($v['QUOTESTATUS']==1){
					$listData[$k]['STATENAME'] = '<span class="status-normal">已报价</span>';
				}elseif($v['CHECKSTATE']==0){
					$listData[$k]['STATENAME'] = '<span class="status-cancel">待报价</span>';
				}
				#省份信息
			
				$strProvince = '';
				if( $v['COUNTRYCODE']){
					$area_len = mb_strlen( $v['COUNTRYCODE']);
					if($area_len==6){
						$strProvince = $Province2[ $v['COUNTRYCODE']];
					}elseif ($area_len==9){
						$strProvince = $Province2[substr( $v['COUNTRYCODE'],0,6)].$City[substr( $v['COUNTRYCODE'],0,6)][ $v['COUNTRYCODE']];
					}elseif ($area_len==12){
						$strProvince = $Province2[substr( $v['COUNTRYCODE'],0,6)].$City[substr( $v['COUNTRYCODE'],0,6)][substr( $v['COUNTRYCODE'],0,9)].$County[substr( $v['COUNTRYCODE'],0,9)][ $v['COUNTRYCODE']];
					} 
				}
				$listData[$k]['ADDRESS'] = $strProvince.$v['ADDRESS'];  
				
			}
		}

		#模板赋值
		$now=time();
		$this->assign('now',$now);
		$this->assign('listData',$listData);
		$this->assign('pageCss',$pageCss);//分页样式
		$this->assign('pageform',$pageform);//分页标签
		$this->assign('type',$type);
		$this->assign('kw',$key);
		$this->assign('nodatainfo',$nodatainfo);
		
		
		$this->assign('menu_navbar','询价管理  -- <a href="'.$_GET['action'].'.html">询价列表</a>');
		$this->tpl_full_path('/admins/enquiry_manage/index');
	}
	
	
	/**
	 * 项目详情预览
	 */
	protected function _view()
	{
		#登录状态可以看到本身所有状态的信息
		$intLoadId = $this->session()->getSession('uid');
		#获取到参数
		$intId = $this->getParam('id');
		$strType = $this->getParam('type');
		$this->assign('strType',$strType);
		
		
		if(empty($intId) || !is_numeric($intId)){
			$this->goTo404();
		}
		
		#获取配置
		$commonConfig = Config::get('common'); 
		$arrProductType = $commonConfig['common']['product_type'];
		#获取到工程信息
		$where='';
		$where.='id = '.$intId;
		$list = new publicGetDataModel();
		$fields = 'id,userId,projectName,projectType,projectArea,countryCode,address,companyName,linkman,tel,mobile,email,leaveWord,systemId,operateTime,firstTime,quoteStatus';
		$arrData = $list->getData($where,$limit = 1,$fields,$order = 'id',$table='sc_enquiry');
		
		if($arrData){
			#工程性质
			if($arrData[0]['PROJECTTYPE']){
			   $arrData[0]['PROJECTTYPENAME'] = $arrProductType[$arrData[0]['PROJECTTYPE']];
			}
			#省份信息
			include_once(SYSTEM_PATH.'libs/data/province.lib.php');
			$strProvince = '';
		    if( $arrData[0]['COUNTRYCODE']){
				$area_len = mb_strlen( $arrData[0]['COUNTRYCODE']);
				if($area_len==6){
					$strProvince = $Province2[ $arrData[0]['COUNTRYCODE']];
				}elseif ($area_len==9){
					$strProvince = $Province2[substr( $arrData[0]['COUNTRYCODE'],0,6)].$City[substr( $arrData[0]['COUNTRYCODE'],0,6)][ $arrData[0]['COUNTRYCODE']];
				}elseif ($area_len==12){
					$strProvince = $Province2[substr( $arrData[0]['COUNTRYCODE'],0,6)].$City[substr( $arrData[0]['COUNTRYCODE'],0,6)][substr( $arrData[0]['COUNTRYCODE'],0,9)].$County[substr( $arrData[0]['COUNTRYCODE'],0,9)][ $arrData[0]['COUNTRYCODE']];
				}
				 $arrData[0]['AREAINFO'] = $strProvince;
			  }
			
			#获取到询价单的产品
			$arrProductData = $this->get_enquiry_product($arrData[0]['ID']);
			if($arrProductData){
				$arrData[0]['PRODUCTDATA'] = $arrProductData;
			}
			$arrData = $arrData[0];
//			print_r($arrData);

		}else{
			$this->goTo404('该信息不存在!');
		}


		$this->assign('arrData',$arrData);
//print_r($arrData);

		$this->assign('menu_navbar','询价管理  -- <a href="'.$_GET['action'].'.html">询价列表 </a> -- 询价详情');
		$this->tpl_full_path('/admins/enquiry_manage/view');
	}
	
	/**
	**获取到询价产品
	*@param $enqId  询价单ID，对应表sc_enquiry.ID
	*/
	private function get_enquiry_product($enqId)
	{
	    if(empty($enqId)){
			return false;
		}
		 $sql = "select id,userId,enqId,productAttr,quote_price,quote_unit,quote_explain,quote_time,firstTime from sc_enquiry_product where enqId = ".$enqId." order by firstTime desc ";
		$arrData = DB::select($sql);
		if($arrData){
			foreach ($arrData as $k=>$v){
				if($v['PRODUCTATTR']){
				   $arrData[$k]['SERPRODUCTATTR'] = unserialize($v['PRODUCTATTR']);
				}
			}
			//var_dump($arrData);
			return $arrData;
		}
	}
	
	/**
	**报价处理
	*/
	public function _offerdo(){
		#获取到参数
		$pid = $this->getParam('pid');
		$eid = $this->getParam('eid');
		$price = $this->getParam('price');
		$remak = $this->getParam('remak');
		$quote_unit = $this->getParam('unit');
		if(empty($pid) || empty($price) ){
			die('error');
		}
		//更新报价信息
		$arrUpdate = array();
		$arrUpdate['quote_price'] = $price;
		$arrUpdate['quote_unit'] = $quote_unit;
		$arrUpdate['quote_explain'] = $remak;
		$arrUpdate['quote_time'] = time();
		$updateStatus = DB::update('sc_enquiry_product','id = '.$pid,$arrUpdate);
		if($updateStatus){
			#判断是否询价单报价情况
			if($eid){
			  $arrQuoteUpdate = array();
			  $checkSql ='select count(*) as cnt from sc_enquiry_product where enqId = '.$eid.' and quote_time = 0';
			  $arrData = DB::select($checkSql);
			  $num = 0;
			  if($arrData){
				$num =  $arrData[0]['CNT']; 
			  }
			  if($num > 0){
				 $arrQuoteUpdate['quoteStatus'] = 0;
			  }else{
				 $arrQuoteUpdate['quoteStatus'] = 1;
			  }
			  $updateQuoteStatus = DB::update('sc_enquiry','id = '.$eid,$arrQuoteUpdate);
			}
			die('ok');
		}else{
			die('error');
		}
		die('error');
	}
	
	
	/**
	 * 获取报价内容
	 */
	protected function  _getpriceinfo(){

		$pid = $this->getParam('pid');
		$unit = $this->getParam('unit');
		
		$unit = $unit?$unit:1;
		
		if(empty($pid) || empty($unit)){
			die('error_null');
		}
		
		
		if($_SERVER["REQUEST_METHOD"] === "POST") {
			$sql = 'select id,userId,quote_price,quote_unit,quote_explain,quote_time from sc_enquiry_product where quote_time > 0 and id  = '.$pid.' limit 1';
			$arrData = DB::select($sql);
			$strPriceVal = '未报价';
			$strPriceExpVal = '';
			$unitlist =  array('','元/m2','元/m2','元/个','元/个','元/m','元/m2');
			if($arrData){
				$strPriceVal = $arrData[0]['QUOTE_PRICE'];
				$strPriceExpVal = $arrData[0]['QUOTE_EXPLAIN'];	
			}
			
			$htmlContent = '';
	
			$htmlContent.= '<div class="price-popup-box" >';
			
			$htmlContent.= '<table cellspacing="1" cellpadding="0" border="0">';
			$htmlContent.= '<tbody>';
			$htmlContent.= '<tr>';
			$htmlContent.= '<td class="one">报价：</td>';
			$htmlContent.= '<td class="two oneh">'.$strPriceVal.'  '.$unitlist[$unit].'</td>';
			$htmlContent.= '</tr>';
			$htmlContent.= '<tr class="twot">';
			$htmlContent.= '<td class="one">附加说明：</td>';
			$htmlContent.= '<td class="two twoh">'.$strPriceExpVal.'</td> ';
			$htmlContent.= '</tr>';
			$htmlContent.= '</tbody>';
			$htmlContent.= '</table>';
			$htmlContent.= ' </div>';
			
			die($htmlContent);
		}
		

		die('error');

	}
	
	




}
?>
